package com.zhuoya.healthcard;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoya.healthcard.contants.Contants;
import com.zhuoya.healthcard.entity.DeliveryInfo;
import com.zhuoya.healthcard.util.HttpUtil;
import com.zhuoya.healthcard.util.ScannerKeyEventHelper;
import com.zhuoya.healthcard.util.ToastUtil;
import com.ldoublem.loadingviewlib.view.LVCircularZoom;

public class PayActivity extends BaseActity implements ScannerKeyEventHelper.OnScanSuccessListener {

    private DeliveryInfo deliveryInfo;
    private LVCircularZoom mLoading;
    private ScannerKeyEventHelper mScannerKeyEventHelper;

    @Override
    public boolean dispatchKeyEvent(final KeyEvent keyEvent) {
        this.mScannerKeyEventHelper.analysisKeyEvent(keyEvent);
        return super.dispatchKeyEvent(keyEvent);
    }

    @Override
    protected void onCreate(final Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_pay);
        this.deliveryInfo = (DeliveryInfo) this.getIntent().getSerializableExtra("deliveryInfo");
        (this.mLoading = this.findViewById(R.id.loading)).setVisibility(View.INVISIBLE);
        this.mScannerKeyEventHelper = new ScannerKeyEventHelper((ScannerKeyEventHelper.OnScanSuccessListener) this);
        this.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            public void onClick(final View view) {
                final Intent intent = new Intent(PayActivity.this,  DeliveryInfoActivity.class);
                final Bundle bundle = new Bundle();
                bundle.putSerializable("info", PayActivity.this.deliveryInfo);
                intent.putExtras(bundle);
                PayActivity.this.startActivity(intent);
                PayActivity.this.finish();
            }
        });
        this.findViewById(R.id.backTV).setOnClickListener(new View.OnClickListener() {
            public void onClick(final View view) {
                final Intent intent = new Intent(PayActivity.this,  DeliveryInfoActivity.class);
                final Bundle bundle = new Bundle();
                bundle.putSerializable("info", PayActivity.this.deliveryInfo);
                intent.putExtras(bundle);
                PayActivity.this.startActivity(intent);
                PayActivity.this.finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        this.mScannerKeyEventHelper.onDestroy();
        super.onDestroy();
    }

    public void onScanSuccess(String replaceAll) {
        replaceAll = replaceAll.replaceAll(" ", "");
        if (replaceAll.length() < 16) {
            Toast.makeText(this, (CharSequence) "获取支付授权码错误，请重试", Toast.LENGTH_SHORT).show();
        }
        final String[] array = {"25", "26", "27", "28", "29", "30"};
        final String substring = replaceAll.substring(0, 2);
        boolean b = false;
        for (int i = 0; i < array.length; ++i) {
            if (array[i].equals(substring)) {
                b = true;
            }
        }
        if (!b) {
            Toast.makeText(this, (CharSequence) "请出示支付付款码", Toast.LENGTH_SHORT).show();
        }
        if (this.mLoading.getVisibility() == View.INVISIBLE) {
            this.runOnUiThread((Runnable) new Runnable() {
                @Override
                public void run() {
                    PayActivity.this.mLoading.setVisibility(View.VISIBLE);
                    PayActivity.this.mLoading.startAnim();
                }
            });
            final String finalReplaceAll = replaceAll;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("authCode: " + finalReplaceAll + "=======================");
                        final JSONObject jsonObject = new JSONObject();
                        jsonObject.put("authCode", finalReplaceAll);
                        jsonObject.put("consignerName", PayActivity.this.deliveryInfo.getReceiptName());
                        jsonObject.put("phone", PayActivity.this.deliveryInfo.getPhone());
                        jsonObject.put("address", PayActivity.this.deliveryInfo.getAddress());
                        jsonObject.put("registrationId", PayActivity.this.deliveryInfo.getRegistrationId());
                        final String submitPostData = HttpUtil.submitPostData(Contants.uri + "pay/tradePay", jsonObject, "utf-8");
                        final JSONObject object = JSON.parseObject(submitPostData);
                        final int code = object.getInteger("code");
                        final String msg = object.getString("msg");
                        final boolean b = true;
                        if (code != 0) { // 支付成功
                            PayActivity.this.runOnUiThread((Runnable) new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        ToastUtil.show(PayActivity.this, msg);
                                        PayActivity.this.startActivity(new Intent(PayActivity.this, MainActivity.class));
                                        PayActivity.this.finish();
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            });
                            return;
                        }
                        final int payType = object.getInteger("pay_type");
                        final String outTradeNo = object.getString("outTradeNo");
                        boolean b2 = b;
                        if (payType == 2) { // 微信免密支付成功
                            b2 = b;
                            if ("SUCCESS".equals(object.getString("payStatus"))) {
                                b2 = false;
                                PayActivity.this.runOnUiThread((Runnable) new Runnable() {
                                    @Override
                                    public void run() {
                                        final Intent intent = new Intent(PayActivity.this, PaySuccessActivity.class);
                                        intent.putExtra("outTradeNo", outTradeNo);
                                        intent.putExtra("authCode", finalReplaceAll);
                                        intent.putExtra("name", deliveryInfo.getMedicalName());
                                        intent.putExtra("expressMoney", deliveryInfo.getExpressMoney());
                                        PayActivity.this.startActivity(intent);
                                        PayActivity.this.finish();
                                    }
                                });
                            }
                        }
                        if (b2) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    final long currentTimeMillis = System.currentTimeMillis();
                                    int i = 1;
                                    while (i != 0) {
                                        try {
                                            final JSONObject jsonObject = new JSONObject();
                                            jsonObject.put("outTradeNo", outTradeNo);
                                            jsonObject.put("payType", payType);
                                            jsonObject.put("expressId", object.getString("expressId"));
                                            jsonObject.put("registrationId", object.getString("registrationId"));
                                            final JSONObject object = JSON.parseObject(HttpUtil.submitPostData(Contants.uri + "pay/payStatasQuery", jsonObject, "utf-8"));
                                            int n = i;
                                            if (object.getInteger("code") == 0) {
                                                n = i;
                                                if ("TRADE_SUCCESS".equals(object.getString("msg"))) {
                                                    n = 0;
                                                    PayActivity.this.runOnUiThread((Runnable) new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            try {
                                                                final Intent intent = new Intent(PayActivity.this,  PaySuccessActivity.class);
                                                                intent.putExtra("outTradeNo", outTradeNo);
                                                                intent.putExtra("authCode", finalReplaceAll);
                                                                intent.putExtra("name", PayActivity.this.deliveryInfo.getMedicalName());
                                                                intent.putExtra("expressMoney", PayActivity.this.deliveryInfo.getExpressMoney());
                                                                PayActivity.this.startActivity(intent);
                                                                PayActivity.this.finish();
                                                            } catch (Exception ex) {
                                                                ex.printStackTrace();
                                                            }
                                                        }
                                                    });
                                                }
                                            }
                                            Thread.sleep(500L);
                                            final long n2 = (System.currentTimeMillis() - currentTimeMillis) / 1000L;
                                            i = n;
                                            if (n2 < 30L) {
                                                continue;
                                            }
                                            i = 0;
                                            try {
                                                PayActivity.this.startActivity(new Intent(PayActivity.this,  MainActivity.class));
                                                PayActivity.this.finish();
                                            } catch (Exception ex) {
                                                ex.printStackTrace();
                                            }
                                            continue;
                                        } catch (Exception ex2) {
                                            PayActivity.this.runOnUiThread((Runnable) new Runnable() {
                                                @Override
                                                public void run() {
                                                    try {
                                                        ToastUtil.show(PayActivity.this, "支付结果查询异常");
                                                        PayActivity.this.startActivity(new Intent(PayActivity.this,  MainActivity.class));
                                                        PayActivity.this.finish();
                                                    } catch (Exception ex) {
                                                        ex.printStackTrace();
                                                    }
                                                }
                                            });
                                            return;
                                        }
                                    }
                                }
                            }).start();
                        }
                    } catch (Exception ex) {
                        PayActivity.this.runOnUiThread((Runnable) new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.show(PayActivity.this, "支付异常");
                                PayActivity.this.startActivity(new Intent(PayActivity.this,  MainActivity.class));
                                PayActivity.this.finish();
                            }
                        });
                    }
                }
            }).start();
        }
    }
}
